package com.cn.dream_order_server.async;

import com.cn.dream_order_server.service.OrderOrderBackstageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Async
public class WlcOrderOrderAsync {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private OrderOrderBackstageService orderOrderBackstageService;

    //签收订单时异步redis任务
    public void orderSignAsync(String orderId,Integer pageNo,Integer thisPage,String userId){
        if (thisPage == 0){
            String s = stringRedisTemplate.opsForValue().get("orderOrder-" + userId +"-"+ pageNo+"-10");
            if (s!=null&&!"".equals(s)){
                stringRedisTemplate.delete("orderOrder-" + userId +"-"+ pageNo+"-10");
                String s1 = stringRedisTemplate.opsForValue().get("orderOrderWaitForReceiving-" + userId + "-all");
                if (s1!=null&&!"".equals(s1)){
                    String[] strings = s1.split(",");
                    for (String str:strings) {
                        stringRedisTemplate.delete(str);
                    }
                    stringRedisTemplate.delete("orderOrderWaitForReceiving-" + userId + "-all");
                }
            }
        }else{
            String s = stringRedisTemplate.opsForValue().get("orderOrderWaitForReceiving-" + userId + "-"+pageNo+"-10");
            if (s!=null&&!"".equals(s)){
                stringRedisTemplate.delete("orderOrderWaitForReceiving-" + userId + "-"+pageNo+"-10");
                String s1 = stringRedisTemplate.opsForValue().get("orderOrder-" + userId + "-all");
                if (s1!=null&&!"".equals(s1)){
                    String[] strings = s1.split(",");
                    for (String str:strings) {
                        stringRedisTemplate.delete(str);
                    }
                    stringRedisTemplate.delete("orderOrder-" + userId + "-all");
                }
            }
        }
    }

    //删除订单时异步redis任务
    public void orderDeleteAsync(String orderId,Integer pageNo,Integer thisPage,String userId){
        switch (thisPage){
            case 0:
                String s = stringRedisTemplate.opsForValue().get("orderOrder-" + userId +"-"+ pageNo+"-10");
                if (s!=null&&!"".equals(s)){
                    stringRedisTemplate.delete("orderOrder-" + userId +"-"+ pageNo+"-10");
                    String s1 = stringRedisTemplate.opsForValue().get("orderOrderAccomplish-" + userId + "-all");
                    if (s1!=null&&!"".equals(s1)){
                        String[] strings = s1.split(",");
                        for (String str:strings) {
                            stringRedisTemplate.delete(str);
                        }
                        stringRedisTemplate.delete("orderOrderAccomplish-" + userId + "-all");
                    }
                    String s2 = stringRedisTemplate.opsForValue().get("orderOrderCancel-" + userId + "-all");
                    if (s1!=null&&!"".equals(s2)){
                        String[] strings = s2.split(",");
                        for (String str:strings) {
                            stringRedisTemplate.delete(str);
                        }
                        stringRedisTemplate.delete("orderOrderCancel-" + userId + "-all");
                    }
                }
                break;
            case 3:
                s = stringRedisTemplate.opsForValue().get("orderOrderAccomplish-" + userId +"-"+ pageNo+"-10");
                if (s!=null&&!"".equals(s)){
                    stringRedisTemplate.delete("orderOrderAccomplish-" + userId +"-"+ pageNo+"-10");
                    String s1 = stringRedisTemplate.opsForValue().get("orderOrderCancel-" + userId + "-all");
                    if (s1!=null&&!"".equals(s1)){
                        String[] strings = s1.split(",");
                        for (String str:strings) {
                            stringRedisTemplate.delete(str);
                        }
                        stringRedisTemplate.delete("orderOrderCancel-" + userId + "-all");
                    }
                    String s2 = stringRedisTemplate.opsForValue().get("orderOrder-" + userId + "-all");
                    if (s1!=null&&!"".equals(s2)){
                        String[] strings = s2.split(",");
                        for (String str:strings) {
                            stringRedisTemplate.delete(str);
                        }
                        stringRedisTemplate.delete("orderOrder-" + userId + "-all");
                    }
                }
                break;
            case 4:
                s = stringRedisTemplate.opsForValue().get("orderOrderCancel-" + userId +"-"+ pageNo+"-10");
                if (s!=null&&!"".equals(s)){
                    stringRedisTemplate.delete("orderOrderCancel-" + userId +"-"+ pageNo+"-10");
                    String s1 = stringRedisTemplate.opsForValue().get("orderOrderAccomplish-" + userId + "-all");
                    if (s1!=null&&!"".equals(s1)){
                        String[] strings = s1.split(",");
                        for (String str:strings) {
                            stringRedisTemplate.delete(str);
                        }
                        stringRedisTemplate.delete("orderOrderAccomplish-" + userId + "-all");
                    }
                    String s2 = stringRedisTemplate.opsForValue().get("orderOrder-" + userId + "-all");
                    if (s1!=null&&!"".equals(s2)){
                        String[] strings = s2.split(",");
                        for (String str:strings) {
                            stringRedisTemplate.delete(str);
                        }
                        stringRedisTemplate.delete("orderOrder-" + userId + "-all");
                    }
                }
                break;
        }
    }


}
